﻿/*
------------------------------------------------------------------------------
This source file is a part of The Two-Layered GUI Toolkit.

Copyright (c) 2011 Florian Haag

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
------------------------------------------------------------------------------
 */
using System;

using TwoLayeredGUI.FlowView;

namespace TwoLayeredGUI.Samples.SampleCore
{
	partial class Example
	{
		public struct FlowItemPartSize
		{
			public FlowItemPartSize(int width, int height)
			{
				this.width = width;
				this.height = height;
			}
			
			private readonly int width;
			
			public int Width {
				get {
					return width;
				}
			}
			
			private readonly int height;
			
			public int Height {
				get {
					return height;
				}
			}
		}
		
		public class FlowItem : IFlowItem
		{
			public FlowItem(FlowItemPartSize[] partSizes, int index)
			{
				if (partSizes == null) {
					throw new ArgumentNullException("partSizes");
				}
				if (partSizes.Length <= 0) {
					throw new ArgumentException("partSizes does not contain any elements.");
				}
				
				this.partSizes = partSizes.CloneTyped();
				this.index = index;
			}
			
			private readonly FlowItemPartSize[] partSizes;
			
			private readonly int index;
			
			public int Index {
				get {
					return index;
				}
			}
			
			private bool forceLineBreakBefore;
			
			public bool ForceLineBreakBefore {
				get {
					return forceLineBreakBefore;
				}
				set {
					forceLineBreakBefore = value;
				}
			}
			
			private bool forceLineBreakAfter;
			
			public bool ForceLineBreakAfter {
				get {
					return forceLineBreakAfter;
				}
				set {
					forceLineBreakAfter = value;
				}
			}
			
			public int PartCount {
				get {
					return partSizes.Length;
				}
			}
			
			public FlowItemPartSize this[int index] {
				get {
					return partSizes[index];
				}
			}
		}
		
		public static FlowItem[] CreateRandomFlowItems()
		{
			Random rdm = new Random();
			
			var result = new FlowItem[rdm.Next(20, 100)];
			for (int i = 0; i < result.Length; i++) {
				var parts = new FlowItemPartSize[rdm.Next(1, 6)];
				for (int p = 0; p < parts.Length; p++) {
					parts[p] = new FlowItemPartSize(rdm.Next(5, 51),
					                                rdm.Next(5, 11));
				}
				var item = new FlowItem(parts, i);
				item.ForceLineBreakBefore = rdm.Next(20) == 0;
				item.ForceLineBreakAfter = rdm.Next(20) == 0;
				result[i] = item;
			}
			
			return result;
		}
	}
}
